package com.cob.view;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.cob.main.COBApplet;
import com.cob.main.COBFactory;
import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.opengl.View;
import com.dalonedrau.jogl.opengl.ui.menu.MenuGroup;
import com.dalonedrau.jogl.opengl.ui.menu.MenuItem;
import com.dalonedrau.jogl.opengl.ui.menu.VerticalMenu;
import com.dalonedrau.jogl.opengl.ui.panel.PagePanel;

/**
 * The title screen view.
 * @author Donald
 */
public final class IntroView implements View {
	/** the panel used to organize the intro. */
	private PagePanel		introPanel;
	/** the title screen menu. */
	private MenuGroup		menu;
	/** the intro text menu. */
	private MenuGroup		menuIntro;
	/** the panel used to organize menu items. */
	private PagePanel		menuPanel;
	/** the intro text scroller. */
	private TextScroller	scroller;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleKeyEvent(final KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.out.println("esc");
			COBApplet.getInstance().exit();
			break;
		case KeyEvent.VK_UP:
			// menu.highlightPrevious();
			break;
		case KeyEvent.VK_DOWN:
			// menu.highlightNext();
			break;
		case KeyEvent.VK_ENTER:
			/*
			 * if (menu != null && menu.isVisible() && menu.getHighlighted() !=
			 * null) { menu.getHighlighted().onSelect(); } else if (scroller !=
			 * null && scroller.isVisible() && scroller.isScrolling()) {
			 * scroller.moveTextToTop(); } else if (menuIntro != null &&
			 * menuIntro.isVisible() && menuIntro.getHighlighted() != null) {
			 * menuIntro.getHighlighted().onSelect(); }
			 */
			break;
		case KeyEvent.VK_TAB:
			/*
			 * if (menu != null) { if (e.isShiftDown()) {
			 * menu.highlightPrevious(); } else { menu.highlightNext(); } }
			 */
			break;
		default:
			break;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		if (menuPanel != null
				&& menuPanel.isVisible()) {
			menuPanel.handleMouseOver(x, y);
		}
		if (introPanel != null
				&& introPanel.isVisible()) {
			introPanel.handleMouseOver(x, y);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMousePress(final MouseEvent e, final float x,
			final float y) {
		if (menuPanel != null
					&& menuPanel.isVisible()) {
			menuPanel.handleMouseClick(e, x, y);
		} else if (introPanel != null
					&& introPanel.isVisible()) {
			introPanel.handleMouseClick(e, x, y);
		}
	}
	/**
	 * Initializes the {@link View}.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		initMenu();
		initIntro();
	}
	/**
	 * Initializes the introductory text display.
	 * @throws Exception if an error occurs
	 */
	private void initIntro() throws Exception {
		menuIntro = new VerticalMenu();
		MenuItem item = COBFactory.getInstance().getStandardMenuItem(
				"DONE");
		item.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				introPanel.setVisible(false);
				menuPanel.setVisible(true);
			}
		});
		menuIntro.addItem(item);
		menuIntro.setVisible(false);
		final float f = 0.75f;
		scroller = new TextScroller(
				GameFont.valueOf(ProjectConstants.getInstance().getFontH1()),
				TextLoader.getInstance().getTextFile("intro"),
						(int) (GlobalConstants.getInstance().getScreenWidth() * f),
						TextScroller.BOTTOM_TO_TOP
				);
		scroller.setBorder(new SNESBorder());
		Engine.getEngine().addEntity(scroller);
		introPanel = new PortraitPagePanel(GuiComponent.CENTER);
		introPanel.addChild(scroller);
		introPanel.addChild(menuIntro);
		Point pt = GlobalMethods.centerComponentInFrame(
				introPanel);
		introPanel.setPosition(pt.x, pt.y, 0.0f);
		scroller.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				final long l = 1500L;
				Engine.getEngine().addDelayedAction(
						new DelayedAction(GlobalMethods.convertMilliToNano(l)) {
							/**
							 * {@inheritDoc}
							 */
							@Override
							public void performAction() {
								menuIntro.setVisible(true);
								menuIntro.highlightIndex(0);
							}
						});
			}
		});
		introPanel.setVisible(false);
	}
	/**
	 * Initializes the start menu.
	 * @throws Exception if an error occurs
	 */
	private void initMenu() throws Exception {
		menu = new VerticalMenu();
		menu.setBorder(new SNESBorder());
		MenuItem item = COBFactory.getInstance().getStandardMenuItem(
				"NEW GAME");
		item.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				GameState.getInstance().nextState();
				// GameState.getInstance().setCurrentState(State.IN_PLAY);
			}
		});
		menu.addItem(item);
		MenuItem item2 = COBFactory.getInstance().getStandardMenuItem(
				"OUR STORY SO FAR...");
		item2.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				menuPanel.setVisible(false);
				scroller.moveTextToBottom();
				scroller.start();
				introPanel.setVisible(true);
				menuIntro.setVisible(false);
			}
		});
		menu.addItem(item2);
		MenuItem item3 = COBFactory.getInstance().getStandardMenuItem(
				"EXIT");
		item3.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				GameApplet.getInstance().fatalError(new Exception("Exit Game"));
			}
		});
		menu.addItem(item3);
		// highlight the 1st index
		menu.highlightIndex(0);
		/*
		 * Sprite titleBanner = Factory.getInstance().getEmptySprite();
		 * titleBanner.setSize(Globals.TITLE_BANNER_WIDTH,
		 * Globals.TITLE_BANNER_HEIGHT); titleBanner.setImage( new ListImage(
		 * COBCanvasListener.getInstance().getIdTitleBanner())); Label lbl = new
		 * Label(titleBanner); Point pt = Globals.centerComponentInFrame(lbl);
		 * lbl.setPosition(new Vector3(pt.x, pt.y, 0.0f)); lbl.setVisible(true);
		 * menuPanel = new PortraitPagePanel(ComponentPortraitAlignment.CENTER);
		 * menuPanel.addChild(lbl); menuPanel.addChild(menu); pt =
		 * Globals.centerComponentInFrame(menuPanel); menuPanel.setPosition(new
		 * Vector3(pt.x, pt.y, 0.0f)); menuPanel.setVisible(true);
		 */
		menuPanel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		menuPanel.addChild(menu);
		Point pt = GlobalMethods.centerComponentInFrame(menuPanel);
		menuPanel.setPosition(pt.x, pt.y, 0.0f);
		menuPanel.setVisible(true);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (menuPanel == null) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		menuPanel.prepareForRendering();
		introPanel.prepareForRendering();
	}
}
